using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_Slime : Enemy, ICounterable
{

    public SlimeDeadState slimeDeadState;

    [SerializeField] bool hasRecoveryAmination = true;
    public bool CanStunned { get => canStunned; }

    [Header("----- Create Child Slime -----")]
    [SerializeField] private int maxCreateCount = 2;
    public Vector2 createMovement = new(8, 4);
    public GameObject childPrefab;
    public float increaseStat = 1.2f;
    public float penaltyStat = .9f;

    protected override void Awake()
    {
        base.Awake();
        idleState = new(this, stateMachine, "Idle");
        moveState = new(this, stateMachine, "Move");
        attackState = new(this, stateMachine, "Attack");
        battleState = new(this, stateMachine, "Battle");
        stunnedState = new(this, stateMachine, "Stunned");
        slimeDeadState = new(this, stateMachine, "Dead");

        animator.SetBool("Stunned_Recovery", hasRecoveryAmination);
    }
    protected override void OnDisable()
    {
        base.OnDisable();
        stateMachine.ChangeState(idleState);
    }
    protected override void OnEnable()
    {
        base.OnEnable();
        stateMachine.InitMachine(idleState);
    }
    //处理敌人眩晕
    public void HanderCounter()
    {
        stateMachine.ChangeState(stunnedState);
    }
    public void CreateChild()
    {
        if (childPrefab == null)
            return;
        bool isRight = false;
        for (int i = 0; i < maxCreateCount; i++)
        {
            isRight = !isRight;
            GameObject child = PoolManager.Instance.Release(childPrefab, (child) =>
            {
                child.transform.position = transform.position;
                child.transform.rotation = Quaternion.identity;

                Enemy_Slime enemySlime = child.GetComponent<Enemy_Slime>();
                enemySlime.SetupSlime(isRight, entityStat);
            });  
        }
    }
    public override void EntityDead()
    {
        base.EntityDead();
        stateMachine.ChangeState(slimeDeadState);
        StopSlowdownEntityCo();
    }
    public void SetupSlime(bool isRight, Entity_Stat parentStat)
    {
        SetVelocityZero();

        float createMoveDir = isRight ? 1 : -1;

        SetVelocity(createMovement.x * createMoveDir, createMovement.y);

        Entity_Stat stat = GetComponent<Entity_Stat>();
        Enemy_Health health = GetComponent<Enemy_Health>();
        
        //修改分身的stat数据
        stat.AdjustStatSetup(parentStat, penaltyStat, increaseStat);

        //更新数据
        health.InitEntityHealth();
    }
}
